package cn.zb.config;

import cn.zb.common.util.RedisUtil;
import cn.zb.common.vo.DictItemVO;
import cn.zb.common.vo.DictVO;
import cn.zb.entity.SysDictData;
import cn.zb.entity.SysDictType;
import cn.zb.mapstruct.DictMapper;
import cn.zb.service.SysDictDataService;
import cn.zb.service.SysDictTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * 字典数据初始化
 *
 * @author: lewin
 * @date: 2024-11-29 18:21
 **/
@Configuration
public class DictInitConfig {
    @Resource
    RedisUtil redisUtil;
    @Resource
    SysDictTypeService typeService;
    @Resource
    SysDictDataService dataService;

    @PostConstruct
    public void initDict(){
        List<SysDictType> types = typeService.list();
        List<DictVO> dictVOS = DictMapper.instance.DICT_VO_LIST(types);

        QueryWrapper<SysDictData> qw = new QueryWrapper<>();
        dictVOS.forEach(dict->{
            qw.clear();
            // 查询字典数据项
            qw.eq("dict_type",dict.getDictType());
            List<SysDictData> dataList = dataService.list(qw);
            // 转vo
            List<DictItemVO> itemVOS = DictMapper.instance.DICT_ITEM_VO_LIST(dataList);
            dict.setItems(itemVOS);
        });
        // 存储在redis中
        redisUtil.set("dict",dictVOS);
    }
}
